package net.steam.easyjoboa.web.action;

import java.util.Date;
import java.util.List;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import net.steam.easyjoboa.biz.DepartmentBiz;
import net.steam.easyjoboa.biz.EmployeeBiz;
import net.steam.easyjoboa.biz.LoginLogBiz;
import net.steam.easyjoboa.biz.PositionBiz;
import net.steam.easyjoboa.biz.RolesBiz;
import net.steam.easyjoboa.entity.Department;
import net.steam.easyjoboa.entity.Employee;
import net.steam.easyjoboa.entity.LoginLog;
import net.steam.easyjoboa.entity.PageBean;
import net.steam.easyjoboa.entity.PageBeans;
import net.steam.easyjoboa.entity.Position;

public class EmployeeAction {

	// 登录名和密码
	private String name;
	private String password;
	private EmployeeBiz employeeBiz;
	private PositionBiz positionBiz;
	private DepartmentBiz departmentBiz;
	private Employee employee;
	private List<Position> positions;
	private List<Department> departments;
	private List<Employee> employees;
	private Integer id;
	private String userName;
	private Integer userId;
	private Integer positionId;
	private Integer departmentId;
	private String oldPwd;
	private String newPwd;
	private String message;
	private RolesBiz rolesBiz;
	private LoginLogBiz loginLogBiz;
	private List<Position> listPositions;
	private List<Department> listDepar;
	private List<Employee> listEmployees;
	private PageBeans pageBeans;
	private int page = 1;
	
	/**
	 * 登录
	 * 
	 * @return
	 */
	// Action跳转参数
	@Action(value = "doLogin", results = {
			@Result(name = "input", location = "/login.jsp"),
			@Result(name = "success", location = "/sys_manager.jsp") })
	public String doLogin() {
		Employee employee = employeeBiz.doLogin(name, password);
		if (employee == null) {
			message = "* 用户名或密码错误";
			return "input";
		}else if(employee.getUserJobstatus()!=0){
			message = "* 用户已离职";
			return "input";
		}else {
			ServletActionContext.getRequest().getSession().setAttribute("loginUser", employee);
			ServletActionContext.getRequest().getSession().setAttribute("employee", employee);
			LoginLog log=new LoginLog();
			log.setEmployee(employee);
			log.setLoginTime(new Date());
			//loginLogBiz.addLog(log);
			return "success";
		}
	}
	
	/**
	 * 查询部门所有员工
	 * @return
	 */
	@Action(value="findEmployeesByDepar",results = {
			@Result(name="success" , location = "/DepartmentManage.jsp")	
			})
	public String findEmployeesByDepar(){
		if(pageBeans==null){
			pageBeans = new PageBeans();
		}
		pageBeans.setCurrentPage(page);
		listEmployees = employeeBiz.findEmployees(pageBeans, employee);
		listDepar = departmentBiz.getAllDepartment();
		return "success";
	}
	
	/**
	 * 查询所有员工
	 * @return
	 */
	@Action(value="findEmployeesExportMe",results = {
			@Result(name="success" , location = "/employeeManage.jsp")	
			})
	public String findEmployeesExportMe(){
		Employee employee = (Employee) ServletActionContext.getRequest().getSession().getAttribute("loginUser");
		listEmployees = employeeBiz.getAllEmployeesExceptLoginUserId(employee.getUserId());
		return "success";
	}
	
	
	
	@Action(value="toAddEmployee",results={@Result(name="success",location="/addEmployee.jsp")})
	public String toAddEmployee(){
		departments = departmentBiz.getAllDepartment();
		return "success";
	}
	
	
	@Action(value="addEmployee",results={@Result(name="success",location="/addEmployeeSuccess.jsp"),
			@Result(name="input",location="/addEmployee.jsp")})
	public String addEmployee(){
		try{
			employee.setPosition(positionBiz.getPositionById(positionId));
			String name = employee.getUserName().trim();
			employee.setUserName(name);
			employee.setUserJobstatus(0);
			employee.setUserCreatedate(new Date());
			id = employeeBiz.addEmployee(employee);
			rolesBiz.updateE(employee);
			return "success";
		}catch (Exception e) {
			e.printStackTrace();
			departments = departmentBiz.getAllDepartment();
			return "input";
		}
	}
	
	
	@Action(value="toEmployeeManager",results={@Result(name="success",location="/employeeManager.jsp")})
	public String toEmployeeManager(){
		try{
			departments = departmentBiz.getAllDepartment();
			employee = (Employee) ServletActionContext.getRequest().getSession().getAttribute("loginUser");
			employees = employeeBiz.finEmployeesByProperty(employee.getUserId(),userName, userId, positionId, departmentId);
		}catch (Exception e) {
			e.printStackTrace();
		}
		return "success";
	}

	
	/**
	 * 员工个人修改信息
	 * @return
	 */
	@Action(value = "updateEmpInf", results = { @Result(name = "success", type = "redirect", location = "getEmpInf") })
	public String updateEmpInf() {
		Employee e = employeeBiz.findEmployeeById(employee.getUserId());
		e.getPosition().setPositionId(employee.getPosition().getPositionId());
		e.setUserBirthday(employee.getUserBirthday());
		e.setUserEmail(employee.getUserEmail());
		e.setUserPhone(employee.getUserPhone());
		e.setUserAddress(employee.getUserAddress());
		e.setUserOfficephone(employee.getUserOfficephone());
		employeeBiz.updateEmployee(e);
		return "success";
	}
	
	@Action(value="delEmployee",results={@Result(location="toEmployeeManager",type="redirectAction")})
	public String delEmployee(){
		employee = employeeBiz.findEmployeeById(id);
		employee.setUserJobstatus(1);
		employeeBiz.updateEmployee(employee);
		return "success";
	}
	
	@Action(value="findEmployee",results={@Result(location="/updateEmployee.jsp")})
	public String findEmployee(){
		employee = employeeBiz.findEmployeeById(id);
		return "success";
	}

	/**
	 * 修改密码
	 * @return
	 */
	@Action(value = "updateEmpPwd", results = {
			@Result(name = "success", type = "redirect", location = "getEmpInf"),
			@Result(name = "input", location = "/Employee_pwd.jsp") })
	public String updateEmpPwd() {
		employee = (Employee) ServletActionContext.getRequest().getSession()
		.getAttribute("loginUser");
		if (oldPwd.equals(employee.getUserPassword())) {
			employee.setUserPassword(newPwd);
			employeeBiz.updateEmployee(employee);
			return "success";
		} else {
			this.message = "旧密码错误,请重新输入!";
			return "input";
		}
	}
	/**
	 * 根据id查找员工信息-查看个人信息
	 * @return
	 */
	@Action(value = "getEmpInf", results = {
			@Result(name = "success", location = "/employeeInfo.jsp")})
	public String getEmpInf() {
		employee = (Employee) ServletActionContext.getRequest().getSession().getAttribute("loginUser");
		employee = employeeBiz.getEmployeeById(employee.getUserId());
		ServletActionContext.getRequest().getSession().setAttribute("loginUser", employee);
		return "success"; 

	}
	
	/**
	 * 根据id查找员工信息-跳转查看个人信息页面
	 * @return
	 */
	@Action(value = "getEmpInfo", results = {
			@Result(name = "success", location = "/employeeInfo.jsp")})
	public String getEmpInfo() {
		employee = employeeBiz.findEmployeeById(id);
		return "success";
	}
	
	/**
	 * 根据id查找员工信息-跳转修改页面
	 * @return
	 */
	@Action(value = "getEmpId", results = {
			@Result(name = "success", location = "/Employee_detail.jsp")})
	public String getByid() {
		employee = (Employee) ServletActionContext.getRequest().getSession().getAttribute("loginUser");
		employee = employeeBiz.getEmployeeById(employee.getUserId());
		ServletActionContext.getRequest().getSession().setAttribute("loginUser", employee);
		return "success";
	}
	
	
	/**
	 * 
	 * 查找所有的用户
	 * @return
	 */
	private PageBean pageBean;
	private List<Employee> list;
	//使用Action零配置
	@Action(value = "findByPage", results = {
			@Result(name = "input", location = "/emp_popedomManager.jsp"),
			@Result(name = "success", location = "/emp_popedomManager.jsp") })
	public String fingByPage() {
		//如果pagebean为空，就new一个
		if (pageBean == null) {
			pageBean = new PageBean();
		}
		list = employeeBiz.findEmpByPage(pageBean, employee);
		if (list.size() == 0) {
			departments = departmentBiz.getAllDepartment();
			list = null;
			return "input";
		} else {
			departments = departmentBiz.getAllDepartment();
			return "success";
		}
	}

	public void setEmployeeBiz(EmployeeBiz employeeBiz) {
		this.employeeBiz = employeeBiz;
	}

	public Employee getEmployee() {
		return employee;
	}

	public void setEmployee(Employee employee) {
		this.employee = employee;
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public void setPositionBiz(PositionBiz positionBiz) {
		this.positionBiz = positionBiz;
	}

	public List<Position> getPositions() {
		return positions;
	}

	public void setPositions(List<Position> positions) {
		this.positions = positions;
	}

	public List<Employee> getEmployees() {
		return employees;
	}

	public void setEmployees(List<Employee> employees) {
		this.employees = employees;
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public Integer getUserId() {
		return userId;
	}

	public void setUserId(Integer userId) {
		this.userId = userId;
	}

	public Integer getPositionId() {
		return positionId;
	}

	public void setPositionId(Integer positionId) {
		this.positionId = positionId;
	}

	public Integer getDepartmentId() {
		return departmentId;
	}

	public void setDepartmentId(Integer departmentId) {
		this.departmentId = departmentId;
	}

	public List<Department> getDepartments() {
		return departments;
	}

	public void setDepartments(List<Department> departments) {
		this.departments = departments;
	}

	public void setDepartmentBiz(DepartmentBiz departmentBiz) {
		this.departmentBiz = departmentBiz;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public void setRolesBiz(RolesBiz rolesBiz) {
		this.rolesBiz = rolesBiz;
	}

	public String getOldPwd() {
		return oldPwd;
	}

	public void setOldPwd(String oldPwd) {
		this.oldPwd = oldPwd;
	}

	public String getNewPwd() {
		return newPwd;
	}

	public void setNewPwd(String newPwd) {
		this.newPwd = newPwd;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public List<Position> getListPositions() {
		return listPositions;
	}

	public void setListPositions(List<Position> listPositions) {
		this.listPositions = listPositions;
	}

	public List<Employee> getListEmployees() {
		return listEmployees;
	}

	public void setListEmployees(List<Employee> listEmployees) {
		this.listEmployees = listEmployees;
	}

	public void setPageBeans(PageBeans pageBeans) {
		this.pageBeans = pageBeans;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public PageBean getPageBean() {
		return pageBean;
	}

	public void setPageBean(PageBean pageBean) {
		this.pageBean = pageBean;
	}

	public List<Employee> getList() {
		return list;
	}

	public void setList(List<Employee> list) {
		this.list = list;
	}

	public PageBeans getPageBeans() {
		return pageBeans;
	}

	public int getPage() {
		return page;
	}

	public List<Department> getListDepar() {
		return listDepar;
	}

	public void setListDepar(List<Department> listDepar) {
		this.listDepar = listDepar;
	}

	public void setLoginLogBiz(LoginLogBiz loginLogBiz) {
		this.loginLogBiz = loginLogBiz;
	}
	
}
